package com.zzyl.controller;


import com.zzyl.base.ResponseResult;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("test/redis")
public class TestRedisController {


    @Resource
    RedisTemplate<String, Object> redisTemplate;
    @GetMapping("put")
    public ResponseResult putValue(){
        redisTemplate.opsForValue().set("name","法外狂徒");
        //指定key的过期时间
        redisTemplate.opsForValue().set("gender","人妖",10, TimeUnit.SECONDS);
        //判断是否存在
        redisTemplate.opsForValue().setIfAbsent("age","18");
        //存hashmap
        HashMap<String, Object> map = new HashMap<>();
        map.put("assignee0", "12254");
        map.put("assignee1", "12252454");
        map.put("assignee2", "12245254");
        redisTemplate.opsForHash().putAll("hashmap",map);
        return ResponseResult.success("成功");

    }
    @GetMapping("get")
    public ResponseResult getValue(){
        Object name = redisTemplate.opsForValue().get("name");
        Object o = redisTemplate.opsForHash().get("hashmap", "assignee0");
        return ResponseResult.success(o);
    }
}
